package pl.elka.simulator.core;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import pl.elka.simulator.core.configurationCommand.ConfigurationCommandType;
import pl.elka.simulator.core.configurationCommand.IConfigurationCommand;
import pl.elka.simulator.core.experiment.ExperimentRunner;
import pl.elka.simulator.core.mobilityManager.IMobilityManager;

public class Simulator {
	public static final float SECTOR_WIDTH = 0.1f;//1 metr
	public final static String enviromentConfigurationId = "enviromentConfigurationId";
	private EnviromentConfiguration enviromentConfiguration = new EnviromentConfiguration();
	
	private Map<String,IMobilityManager> managers = new HashMap<String, IMobilityManager>();
	
	public void addMobilityManager(IMobilityManager manager){
		managers.put(manager.getId(), manager);
	}
	public void executeConfigurationCommand(IConfigurationCommand cmd, String mobilityManagerId){
		if(enviromentConfigurationId.equals(mobilityManagerId)){
			cmd.execute(enviromentConfiguration);
		}
		else{
			cmd.execute(
					managers.get(mobilityManagerId)
			);
		}
	}
	public List<ConfigurationCommandType> getAllowedConfigurationCommands(String mobilityManagerId){
		if(enviromentConfigurationId.equals(mobilityManagerId)){
			return enviromentConfiguration.getAllowedConfigurationCommands();
		}
		else{
			return managers.get(mobilityManagerId).getAllowedConfigurationCommands();
		}
	}
	public List<String> getMobilityManagers(){
		return new ArrayList<String>(managers.keySet());
	}
	public List<IMobilityManager> getMobilityManagersFull(){
		return new ArrayList<IMobilityManager>(managers.values());
	}
	public List<IPaintable> getPaintableElements() {
		List<IPaintable> ret = new ArrayList<IPaintable>();
		ret.addAll(enviromentConfiguration.getPaintaibleElements());
		for(IMobilityManager mm:managers.values()){
			ret.addAll(mm.getPaintaibleElements());
		}		
		return ret;
	}
	public EnviromentConfiguration getEnviromentConfiguration() {
		return enviromentConfiguration;
	}
	public void setEnviromentConfiguration(
			EnviromentConfiguration enviromentConfiguration) {
		this.enviromentConfiguration = enviromentConfiguration;
	}
	public void runSimulation(String managerId) {	
		for(Map.Entry<String, IMobilityManager> e : managers.entrySet()){
			e.getValue().clear();
		}
		
		new ExperimentRunner().run(enviromentConfiguration, managers.get(managerId), 350);//900);//15 minut
	}
	
}
